<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小人快跑</title>
    <style>
        html,body{
            width: 100%;
            height: 100%;
        }
        *{margin: 0;padding: 0;}
        img{
            animation: run 6s 0.2s 3 forwards paused;
        }
        p{
            text-align: center;
        }
        p button{
            width: 100px;
            margin: 0 20px;
        }
        @keyframes run{
            0%{
                transform: translate(0,0); 
            }
            100%{
                transform: translate(1200px,0); 
            }  
        }
    </style>
</head>
<body>
    <img src="./imgs/2/1.jpeg" alt="">
    <p>
        <button id="start">开始</button>
        <button id="stop">停止</button>
        <!-- <button id="back">掉头</button> -->
    </p>
   

    <script>
        let img = document.querySelector('img');
        let startBtn = document.querySelector('#start');
        let stopBtn = document.querySelector('#stop');
        // let backBtn = document.querySelector('#back');
        let timer;
        let n = 1;
        function run(){
            timer = setInterval(function(){
                img.src = `./imgs/2/${n}.jpeg`;
                n++;
                n === 9 ? n = 1 : n = n;
            },100); 
        };
        
        startBtn.addEventListener('click',function(){
            this.disabled = true;
            stopBtn.disabled = false;
            // backBtn.disabled = false;
            img.style.animationPlayState = 'running';
            run();
        });
        stopBtn.addEventListener('click',function(){
            this.disabled = true;
            startBtn.disabled = false;
            // backBtn.disabled = false;
            img.style.animationPlayState = 'paused';
            clearInterval(timer);
        });
        // backBtn.addEventListener('click',function(){
        //     this.disabled = true;
        //     startBtn.disabled = false;
        //     stopBtn.disabled = false;
        //     img.style.transform = 'rotateY(180deg)';
        //     img.style.animationDirection = 'a'
        //     // clearInterval(timer);
        // });

    </script>
</body>
</html>